package com.bjpowernode.service.impl;

import com.bjpowernode.constant.Constants;
import com.bjpowernode.entity.TActivity;
import com.bjpowernode.entity.TActivityRemark;
import com.bjpowernode.entity.TUser;
import com.bjpowernode.mapper.TActivityRemarkMapper;
import com.bjpowernode.query.ActivityRemarkQuery;
import com.bjpowernode.service.ActivityRemarkService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class ActivityRemarkServiceImpl implements ActivityRemarkService {

    @Resource
    private TActivityRemarkMapper tActivityRemarkMapper;

    @Override
    public int saveActivityRemark(ActivityRemarkQuery activityRemarkQuery) {
        TActivityRemark tActivityRemark = new TActivityRemark();
        BeanUtils.copyProperties(activityRemarkQuery, tActivityRemark);
        //创建时间
        tActivityRemark.setCreateTime(new Date());
        //创建人
        TUser authUser = (TUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer currentLoginUserId = authUser.getId();
        tActivityRemark.setCreateBy(currentLoginUserId);
        return tActivityRemarkMapper.insertSelective(tActivityRemark);
    }

    @Override
    public PageInfo<TActivityRemark> getActivityRemarkPage(Integer current, Integer activityId) {
        //1.设置PageHelper (第一个参数是查第几页，第二个参数每页查多少条数据)
        PageHelper.startPage(current, Constants.PAGE_SIZE);

        //2.查询数据库表的数据
        List<TActivityRemark> list = tActivityRemarkMapper.selectActivityRemarkPage(activityId);

        //3.封装分页数据到PageInfo
        PageInfo<TActivityRemark> info = new PageInfo<>(list);

        //4.返回PageInfo对象
        return info;
    }

    @Override
    public TActivityRemark getActivityRemarkById(Integer id) {
        return tActivityRemarkMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateActivityRemark(ActivityRemarkQuery activityRemarkQuery) {
        TActivityRemark tActivityRemark = new TActivityRemark();
        BeanUtils.copyProperties(activityRemarkQuery, tActivityRemark);
        //编辑时间
        tActivityRemark.setEditTime(new Date());
        //编辑人
        TUser authUser = (TUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer currentLoginUserId = authUser.getId();
        tActivityRemark.setEditBy(currentLoginUserId);
        return tActivityRemarkMapper.updateByPrimaryKeySelective(tActivityRemark);
    }

    @Override
    public int delActivityRemarkById(Integer id) {
        //逻辑删除：数据不删，只是修改一下该数据的状态，数据依然在数据库中
        //物理删除：真的把数据删了，数据库就没有这条数据了
        TActivityRemark tActivityRemark = new TActivityRemark();
        tActivityRemark.setId(id);
        tActivityRemark.setDeleted(1); // 删除状态（0或者null正常，1删除）
        return tActivityRemarkMapper.updateByPrimaryKeySelective(tActivityRemark);
    }
}
